package com.css.zfzx.fwsjcj.modules.user.repository.impl;

import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.constants.YNEnum;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.user.entity.FwUserEntity;
import com.css.zfzx.fwsjcj.modules.user.repository.FwQueryRespository;
import com.css.zfzx.fwsjcj.modules.user.vo.UserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.List;

@Repository
public class FwUserResPository implements FwQueryRespository {
    @PersistenceContext
    EntityManager entityManager;
    @Autowired
    private DivisionRepository divisionRepository;
    @Override
    public Page<FwUserEntity> queryUser(HttpServletRequest request,UserQueryVo userQueryVo, int curPage, int pageSize) {
       //DeptEntity deptEntity= ServerUtil.getMainDept(request);
        StringBuilder sql=new StringBuilder("select * from fw_user ");
        StringBuilder whereSql=new StringBuilder("where is_valid = '"+ Constant.IS_VALID_YES+"'");
        if(!StringUtils.isEmpty(userQueryVo.getRelName())){
            whereSql.append(" and rel_name like :relName");
        }
        if(!StringUtils.isEmpty(userQueryVo.getMobilePhone())){
            whereSql.append(" and mobile_phone like :mobilePhone");
        }
        if(!StringUtils.isEmpty(userQueryVo.getStatus())){
            whereSql.append(" and status = :status");
        }
        if(!StringUtils.isEmpty(userQueryVo.getUserType())){
            whereSql.append(" and user_type = :userType");
        }
        String userId= PlatformSessionUtils.getUserId();
        DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
        if(!StringUtils.isEmpty(mainDept)) {
            String divisionId = mainDept.getDivisionId();
            DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
            if(!StringUtils.isEmpty(validDivisionById)) {
                if(!"中华人民共和国".equals(validDivisionById.getDivisionName())) {
                    whereSql.append(" and user_address like :divisionName");
                }
            }
        }
        whereSql.append(" and status != '"+ Constant.STATUS_SUCESS_SH+"'");
       /* if(!StringUtils.isEmpty(deptEntity)) {
            if (!StringUtils.isEmpty(deptEntity.getDivisionId())) {
                whereSql.append(" and division = :division");
            }
        }*/
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).append(orderSql).toString(), FwUserEntity.class);

        StringBuilder countSql =new StringBuilder("select count(*) from fw_user ");
        Query countQuery = entityManager.createNativeQuery(countSql.append(whereSql).toString());

        if(!StringUtils.isEmpty(userQueryVo.getRelName())){
            nativeQuery.setParameter("relName","%"+userQueryVo.getRelName()+"%");
            countQuery.setParameter("relName","%"+userQueryVo.getRelName()+"%");
        }
        if(!StringUtils.isEmpty(userQueryVo.getMobilePhone())) {
            nativeQuery.setParameter("mobilePhone", "%" + userQueryVo.getMobilePhone() + "%");
            countQuery.setParameter("mobilePhone", "%" + userQueryVo.getMobilePhone() + "%");
        }
        if(!StringUtils.isEmpty(userQueryVo.getStatus())){
            nativeQuery.setParameter("status",userQueryVo.getStatus());
            countQuery.setParameter("status",userQueryVo.getStatus());
        }
        if(!StringUtils.isEmpty(userQueryVo.getUserType())){
            nativeQuery.setParameter("userType",userQueryVo.getUserType());
            countQuery.setParameter("userType",userQueryVo.getUserType());
        }
        if(!StringUtils.isEmpty(mainDept)) {
            String divisionId = mainDept.getDivisionId();
            DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
            if(!StringUtils.isEmpty(validDivisionById)) {
                if(!"中华人民共和国".equals(validDivisionById.getDivisionName())) {
                    String divisionName = validDivisionById.getDivisionName();
                    nativeQuery.setParameter("divisionName", "%" + divisionName + "%");
                    countQuery.setParameter("divisionName", "%" + divisionName + "%");
                }
            }
        }
        Pageable pageable= PageRequest.of(curPage-1,pageSize);
        nativeQuery.setFirstResult(pageable.getPageNumber()*pageable.getPageSize());
        nativeQuery.setMaxResults(pageable.getPageSize());
        List<FwUserEntity> list=nativeQuery.getResultList();
        BigInteger count =(BigInteger) countQuery.getSingleResult();
        Page<FwUserEntity> page=new PageImpl<>(list,pageable,count.longValue());
        return page;
    }
}
